package com.foreknow.demo12.exer;

/**
 * @author shkstart
 * @create 2021-08-02 13:01
 */

import java.util.*;

/**
 * 一、有一个字符串数组,
 * {"aaa","ccc","vvv","nnn","aaa","ccc","vvv","ddd","eee","eee","ddd","ccc","aaa","vvv","nnn"},
 * 使用最少的代码统计出每个字符串对象出现的次数
 * <p>
 * <p>
 * 二、实现一个随机分组的程序, 随便选择今年NBA中16支队伍, 然后将这16支队伍放进一个List里面,
 * 并在控制台上随机分成4组, 打印出来
 * String nba []= {"骑士","勇士","湖人","热火","老鹰","猛龙","篮网","凯尔特人","小牛","活塞",
 * "公牛","魔术","爵士","开拓","火箭"};
 */
public class ListMapTest {
    public static void main(String[] args) {
        int a = 0;
        int k = 1;//出现次数
        Map<String, Integer> map = new HashMap<>();
        //String[] keys= (String[]) map.keySet().toArray();
        String[] arr1 = {"aaa", "ccc", "vvv", "nnn", "aaa", "ccc", "vvv", "ddd", "eee", "eee", "ddd", "ccc", "aaa", "vvv", "nnn"};
        for (int i = 0; i < arr1.length; i++) {
            if (map.containsKey(arr1[i])) {
                map.put(arr1[i], k);
                Integer integer = map.get(i);
//                num++;
//                map.put(arr1[i],num);

            }else {
                map.put(arr1[i], k);

            }
        }


//key用来存数据   value用来存次数    有问题？？待解决
        //如果不在map中就添加找到map中  如果在对应的value就加1
        for (int i = 0; i < arr1.length; i++) {
            Set<Map.Entry<String, Integer>> entries = map.entrySet();
            if (map.containsKey(arr1[i])) {
                k++;
            } else if (!map.containsKey(arr1[i])) {
                map.put(arr1[i], k);
            }

        }


        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> next = iterator.next();
            System.out.println(next.getKey() + "--------------" + next.getValue());
        }
    }


}





